﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using TangoGames.Base;
using TangoGames.Scene;
using incubation_xna3d.Scene;

namespace incubation_xna3d.HUD
{
    public class Hud
    {
        int saudepersonagem = 100;
        int quantidadedemunicao = 90;
        int quantidadecartucho = 30;
        SpriteFont FonteArma;

        IInputService input;
        ISceneManager sceneControl;

        GameWindow window;
        Texture2D hudbase;
        Texture2D saude;
        Texture2D escudo;
        Texture2D barrasaude;
        Texture2D barrasaudeelemento;
        Texture2D m16;
        Texture2D mira;
        

        //botao para o menu
        Texture2D botaomenu;
        public Boolean chamoumenu = false;

        // Controle de camera
        Texture2D viradireita;
        Texture2D viraesquerda;

        //Controle de turno
        Texture2D passaturno;
        Texture2D proximochar;
        Texture2D anteriorchar;
        Texture2D divisor;

              
        IList<Texture2D> listasaude;

        public Hud(ContentManager content,GameWindow window,GameServiceContainer services, ISceneManager sceneControl)
        {
            hudbase = content.Load<Texture2D>("Textures/HUD/hud");
            botaomenu = content.Load<Texture2D>("Textures/HUD/botaomenu");
            saude = content.Load<Texture2D>("Textures/HUD/saude");
            escudo = content.Load<Texture2D>("Textures/HUD/escudo");
            mira = content.Load<Texture2D>("Textures/HUD/mira");
            barrasaude = content.Load<Texture2D>("Textures/HUD/barradesaude");
            barrasaudeelemento = content.Load<Texture2D>("Textures/HUD/barradesaudeelemento");
            m16 = content.Load<Texture2D>("Textures/HUD/m16");
            FonteArma = content.Load<SpriteFont>("Textures/HUD/FonteArma");
            viraesquerda = content.Load<Texture2D>("Textures/HUD/viraesq");
            viradireita = content.Load<Texture2D>("Textures/HUD/viradir");
            passaturno = content.Load<Texture2D>("Textures/HUD/nextturnbutton");
            proximochar = content.Load<Texture2D>("Textures/HUD/proximo");
            anteriorchar = content.Load<Texture2D>("Textures/HUD/anterior");
            divisor = content.Load<Texture2D>("Textures/HUD/divisoria");

            this.window = window;
            this.sceneControl = sceneControl;

            input = (IInputService)services.GetService(typeof(IInputService));

            listasaude = new List<Texture2D>();

            for (int i = 0; i < 5; i++)
            {
                listasaude.Add(barrasaudeelemento);
                               
            }


        }

        public void Update()
        {
            if(input.KeyPressOnce(Keys.K)){

               // saudepersonagem -= 5;
            }


            var c = sceneControl.CaracterControl.CurrentCaracter;
            //Console.WriteLine("Soldier: attack = " + c.AttackPower + "; health = " + c.Health + "; movement points = " + c.MovementPoints);

            saudepersonagem = c.Health;
            //ControleSaude();
            Botoes();
            
        }

        

        public void Draw(SpriteBatch spritebatch){

            
            spritebatch.Draw(hudbase,new Vector2(window.ClientBounds.Width/2 - hudbase.Width/2,window.ClientBounds.Height + 10 - hudbase.Height), Color.White);
            spritebatch.Draw(saude, MouseOver( LocalSaude()), Color.White);                        
            //spritebatch.Draw(mira, Localmira(),Color.White);
            spritebatch.Draw(mira, MouseOver(Localmira()), Color.White);
            spritebatch.Draw(escudo, MouseOver( LocalEscudo()), Color.White);
            spritebatch.Draw(viraesquerda, MouseOver( Localturnleft()), Color.White);
            spritebatch.Draw(viradireita,  MouseOver(Localturnright()), Color.White);

            spritebatch.Draw(passaturno, MouseOver(localpassaturno()), Color.White);
            spritebatch.Draw(divisor,new Rectangle (window.ClientBounds.Width / 2 + 545, window.ClientBounds.Height - hudbase.Height + 89 + passaturno.Height, divisor.Width, divisor.Height), Color.White);
            spritebatch.Draw(proximochar,MouseOver(Localproximo()), Color.White);
            spritebatch.Draw(anteriorchar,MouseOver(Localanterior()), Color.White);

            spritebatch.Draw(botaomenu, MouseOver(Localmenu()), Color.White);

            // draw da arma e munição
            spritebatch.Draw(m16, new Vector2(window.ClientBounds.Width / 2 - 100, window.ClientBounds.Height - hudbase.Height +125), Color.White);
            spritebatch.DrawString(FonteArma, quantidadecartucho.ToString() + "/" + quantidadedemunicao.ToString(), new Vector2(window.ClientBounds.Width / 2 + 40, window.ClientBounds.Height - hudbase.Height + 160), Color.White);


            // draw da barra de saude
            for (int i = 0; i < 5; i++)
            {
                Color cor = new Color();
                Boolean condicao = false;
                cor = ControleSaude(i,out condicao);
                int posicaovalor = i * 67;
                if (condicao == true)
                {
                    spritebatch.Draw(listasaude[i], new Vector2(window.ClientBounds.Width / 2 - 550 + posicaovalor, window.ClientBounds.Height - hudbase.Height + 135), cor);
                }
            }

            
            spritebatch.Draw(barrasaude, new Vector2(window.ClientBounds.Width / 2 - 550, window.ClientBounds.Height - hudbase.Height + 120), Color.White);

        }

        #region controle da barra de saude
        private Color ControleSaude(int i,out Boolean condicao)
        {
            Color cordesenho = new Color();
            

            //for (int i = 0; i < 5; i++)
            //{
            condicao = false;
                if (i == 0)
                {
                    if (saudepersonagem <= 100 && saudepersonagem > 10)
                    {
                        cordesenho = Color.Green;
                        //DesenhaSaude(listasaude[i], cor);
                        condicao = true;
                    }
                    else if (saudepersonagem > 0 && saudepersonagem <= 10)
                    {
                        cordesenho = Color.Red;
                        //DesenhaSaude(listasaude[i], cor);
                        condicao = true;
                    }
                }
                else if (i == 1)
                {
                    if (saudepersonagem <= 100 && saudepersonagem > 30)
                    {
                        cordesenho = Color.Green;
                        //DesenhaSaude(listasaude[i], cor);
                        condicao = true;
                    }
                    else if (saudepersonagem > 20 && saudepersonagem <= 30)
                    {
                        cordesenho = Color.Red;
                        //DesenhaSaude(listasaude[i], cor);
                        condicao = true;
                    }
                }
                else if (i == 2)
                {
                    if (saudepersonagem <= 100 && saudepersonagem > 50)
                    {
                        cordesenho = Color.Green;
                        //DesenhaSaude(listasaude[i], cor);
                        condicao = true;
                    }
                    else if (saudepersonagem > 40 && saudepersonagem <= 50)
                    {
                        cordesenho = Color.Red;
                        //DesenhaSaude(listasaude[i], cor);
                        condicao = true;
                    }
                }
                else if (i == 3)
                {
                    if (saudepersonagem <= 100 && saudepersonagem > 70)
                    {
                        cordesenho = Color.Green;
                        //DesenhaSaude(listasaude[i], cor);
                        condicao = true;
                    }
                    else if (saudepersonagem > 60 && saudepersonagem <= 70)
                    {
                        cordesenho = Color.Red;
                        //DesenhaSaude(listasaude[i], cor);
                        condicao = true;
                    }
                }
                else if (i == 4)
                {
                    if (saudepersonagem <= 100 && saudepersonagem > 90)
                    {
                        cordesenho = Color.Green;
                        //DesenhaSaude(listasaude[i], cor);
                        condicao = true;
                    }
                    else if (saudepersonagem > 80 && saudepersonagem <= 90)
                    {
                        cordesenho = Color.Red;
                        //DesenhaSaude(listasaude[i], cor);
                        condicao = true;
                    }
                }


                return cordesenho;
            
                      
        }
        #endregion

        #region Botões

        private void Botoes()
        {
            //chamaar menu
            if (input.MouseClick(Localmenu()))
            {
                chamoumenu = true;


            }




            // botao do escudo
            if (input.MouseClick(LocalEscudo()))
            {
                //var c = sceneControl.CaracterControl.CurrentCaracter;

                Console.WriteLine("clicou escudo");
                

            }
            //botao da saude
            if (input.MouseClick(LocalSaude()))
            {
                //var c = sceneControl.CaracterControl.CurrentCaracter;

                Console.WriteLine("clicou saude");
            }
            if (input.MouseClick(Localmira()))
            {
                Console.WriteLine("clicou mira");


            }          
            ////botao de vira camera p esquerda
            //if (input.MouseClick(Localturnleft()))
            //{
            //    Console.WriteLine("clicou vira esquerda");
            //}
            ////botao de vira camera p direita
            //if (input.MouseClick(Localturnright()))
            //{
            //    Console.WriteLine("clicou vira direita");
            //}
            //botao de fim de turno
            //if (input.MouseClick(localpassaturno()))
            //{
            //    Console.WriteLine("clicou FIM DE TURNO");
            //}
            //botao de proximo personagem
            if (input.MouseClick(Localproximo()))
            {
                Console.WriteLine("clicou proximo personagem");
            }
            //botao de personagem anterior
            if (input.MouseClick(Localanterior()))
            {
                Console.WriteLine("clicou personagem anterior");
            }


        }

        #endregion 

        #region posição dos elementos

        public Rectangle Localmenu()
        {
            return new Rectangle(window.ClientBounds.Width / 2 + 358, window.ClientBounds.Height - hudbase.Height + 185, botaomenu.Width, botaomenu.Height);
        }

        public Rectangle LocalEscudo()
        {
            return new Rectangle(window.ClientBounds.Width / 2 + 270, window.ClientBounds.Height - hudbase.Height + 80, escudo.Width, escudo.Height);
        }
        public Rectangle LocalSaude()
        {
            return new Rectangle(window.ClientBounds.Width / 2 + 355, window.ClientBounds.Height - hudbase.Height + 80, saude.Width,saude.Height);
        }
        public Rectangle Localmira()
        {
            return new Rectangle(window.ClientBounds.Width / 2 + 445, window.ClientBounds.Height - hudbase.Height + 80, saude.Width, saude.Height);
        }
        public Rectangle Localturnright()
        {
            return new Rectangle(window.ClientBounds.Width / 2 + 455, window.ClientBounds.Height - hudbase.Height + 175, viradireita.Width, viradireita.Height);
        }
        public Rectangle Localturnleft()
        {
            return new Rectangle(window.ClientBounds.Width / 2 + 270, window.ClientBounds.Height - hudbase.Height + 175, viraesquerda.Width, viraesquerda.Height);
        }
        public Rectangle localpassaturno()
        {
            return new Rectangle(window.ClientBounds.Width / 2 + 545, window.ClientBounds.Height - hudbase.Height + 90, passaturno.Width, passaturno.Height);
        }
        public Rectangle Localanterior()
        {
            return new Rectangle(window.ClientBounds.Width / 2 + 545, window.ClientBounds.Height - hudbase.Height + 97 + passaturno.Height, anteriorchar.Width, anteriorchar.Height);
        }
        public Rectangle Localproximo()
        {
            return new Rectangle(window.ClientBounds.Width / 2 + 550 + anteriorchar.Width, window.ClientBounds.Height - hudbase.Height + 97 + passaturno.Height, proximochar.Width, proximochar.Height);
        }


        #endregion

        #region Mouse over

        // verifica se o mouse esta sobre o botao para
        private Rectangle MouseOver(Rectangle inicial)
        {

            Rectangle destination;
            destination = new Rectangle();
            destination = inicial;
            
            // verifica se o mouse esta sobre o retangulo
            if (input.MouseOver(destination))
            {
                
                destination.X -= 2;
                destination.Y -= 2;
                destination.Width += 5;
                destination.Height += 5;


            }

            return destination;
        }
        #endregion 

    }
}
